﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace LibExtend
{
    public partial class DragForm : Form
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        public enum WMessages : int
        {
            WM_LBUTTONDOWN = 0x201, //Left mousebutton down
            WM_LBUTTONUP = 0x202,  //Left mousebutton up
            WM_LBUTTONDBLCLK = 0x203, //Left mousebutton doubleclick
            WM_RBUTTONDOWN = 0x204, //Right mousebutton down
            WM_RBUTTONUP = 0x205,   //Right mousebutton up
            WM_RBUTTONDBLCLK = 0x206, //Right mousebutton doubleclick
            WM_KEYDOWN = 0x100,  //Key down
            WM_KEYUP = 0x101,   //Key up
        }

        public delegate void FormDraggedHandler(TableView TableView, Table Item, bool Dragged, Point MousePoint);
        public event FormDraggedHandler FormDragged;

        private Table _Table;
        public Table Table
        {
            get { return _Table; }
            set 
            {
                if (value == null) return;

                _Table = value;
                this.BackgroundImage = _Table.GetSmallImage();
            }
        }

        public TableView TableView { get; set; }

        public DragForm()
        {
            InitializeComponent();
        }

        private void DragForm_Paint(object sender, PaintEventArgs e)
        {
            Pen p = new Pen(Color.Gray, 3);
            e.Graphics.DrawRectangle(p, 0, 0, this.Width - 1, this.Height - 1);
            p.Dispose();
        }

        Point lastClick;//Holds where the Form was clicked

        private void DragForm_MouseDown(object sender, MouseEventArgs e)
        {
            lastClick = new Point(e.X, e.Y); //We'll need this for when the Form starts to move
        }

        private void DragForm_MouseMove(object sender, MouseEventArgs e)
        {
            //Point newLocation = new Point(e.X - lastE.X, e.Y - lastE.Y);
            if (e.Button == MouseButtons.Left) //Only when mouse is clicked
            {
                //Move the Form the same difference the mouse cursor moved;
                this.Left += e.X - lastClick.X;
                this.Top += e.Y - lastClick.Y;

                if (FormDragged != null)
                    FormDragged(this.TableView, Table, true, new Point(this.Location.X + e.X, this.Location.Y + e.Y));
            }

        }

        private void DragForm_MouseUp(object sender, MouseEventArgs e)
        {
            this.Hide();
            if (FormDragged != null)
                FormDragged(this.TableView, Table, false, new Point(this.Location.X + e.X, this.Location.Y + e.Y));
        }

        public void _SendMessage(int x, int y)
        {
            SendMessage(this.Handle, (int)WMessages.WM_LBUTTONDOWN, 0, MakeLParam(x, y));
            if(Table != null) lbl.Text = Table.TenBan;
        }

        private int MakeLParam(int LoWord, int HiWord)
        {
            return ((HiWord << 16) | (LoWord & 0xffff));
        }

        private void DragForm_Resize(object sender, EventArgs e)
        {
            if (this.Width != 66)
                this.Width = 66;
            if (this.Height != 64)
                this.Height = 64;
        }

        private void DragForm_Load(object sender, EventArgs e)
        {
        }
    }
}
